package com.peppa.petshop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.peppa.petshop.entity.Address;
import com.peppa.petshop.service.AddressService;
import com.peppa.petshop.vo.AddressVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张三
 * @since 2021-06-02
 */
@Controller
@RequestMapping("/user")
public class AddressController {
    @Autowired
    private AddressService addressService;
    @RequestMapping("/addressList")
    @ResponseBody
    @CrossOrigin
    public List<AddressVO> getAddressList(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();
        System.out.println("我来看我的地址了");
//        Integer customerId = (Integer) session.getAttribute("customerId");
//        System.out.println(customerId);
        Integer customerId = new Integer("2");
        List<AddressVO>  addressVOList =  addressService.getAddressList(customerId);
        response.setHeader("Access-Control-Allow-Credentials", "true");
        return addressVOList;
    }
    @PostMapping("/addAddress")
    @ResponseBody
    @CrossOrigin
    public String addAddress(HttpServletRequest request,String adduser,String addphone,String addname){
        HttpSession session = request.getSession();
        System.out.println("添加地址");
        System.out.println(adduser);
        System.out.println(addphone);
        System.out.println(addname);
//        Integer customerId = (Integer) session.getAttribute("customerId");
        Integer customerId = new Integer("2");
        Address address = new Address();
        address.setAddressName(addname);
        address.setCustomer(customerId);
        address.setUsername(adduser);
        address.setPhone(addphone);
        System.out.println(customerId);
        address.setCreateTime(LocalDateTime.now());
        address.setUpdateTime(LocalDateTime.now());
        addressService.addAddress(address);
        return "redirect:/user/addressList";
    }
    @PostMapping("/addDefault")
    @ResponseBody
    @CrossOrigin
    public String addDefault(HttpServletRequest request,Integer addressId){
        System.out.println("assss:"+addressId);
        System.out.println(request.getParameter("customerId"));
        String flag =addressService.addDefault(addressId);
        return flag;
    }

}

